package cm.app.user.action;

import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import cm.app.user.form.UserForm;
import cm.app.user.service.UserService;
import cm.fw.base.CommonActionBean;
import cm.fw.utility.Const;

public class UserActionBean
        extends CommonActionBean {
    /** ユーザサービス */
    @SpringBean("userService")
    private UserService userService;

    private UserForm    form;

    @HandlesEvent("load")
    public Resolution load()
            throws Exception {
        form.setLoginId("");
        form.setName("");
        form.setOrganizationId("");
        form.setOrganizationName("");
        getContext().getRequest().setAttribute("form", form);
        return new ForwardResolution("jsp/user/user.jsp");
    }

    @HandlesEvent("search")
    public Resolution search()
            throws Exception {
        form.setPageCount(Const.PAGE_COUNT);
        if (form.getPageNo() == null || "".equals(form.getPageNo())) {
            form.setPageNo("0");
        } else
            if ("add".equals(form.getTurnPageFlag())) {
                form.setPageNo(String.valueOf(Integer.parseInt(form.getPageNo()) + 1));
            } else
                if ("sub".equals(form.getTurnPageFlag())) {
                    form.setPageNo(String.valueOf(Integer.parseInt(form.getPageNo()) - 1));
                }

        // データを取る
        form.setData(userService.doSearch(form));
        form.setPageCount(Const.PAGE_COUNT);

        // 全部データの数量
        form.setDataCount(String.valueOf(userService.doCountAll(form)));
        getContext().getRequest().setAttribute("form", form);
        return new ForwardResolution("jsp/user/userPart.jsp");
    }

    @HandlesEvent("delete")
    public Resolution delete()
            throws Exception {
        userService.delete(form);
        search();
        return new ForwardResolution("jsp/user/userPart.jsp");
    }

    public void setForm(UserForm form) {
        this.form = (UserForm) form;
    }

}
